package cn.psh.system.service.impl;

import cn.psh.system.domain.Menu;
import cn.psh.system.mapper.MenuMapper;
import cn.psh.system.service.IMenuService;
import cn.psh.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.hash.HashMapper;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author pider
 * @since 2022-09-20
 */
@Service
public class MenuServiceImpl extends BaseServiceImpl<Menu> implements IMenuService {

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public List<Menu> queryTree() {
        List<Menu> menus = menuMapper.queryAll();
        Map<Long, Menu> map = new HashMap<>();
        for (Menu menu : menus) {
            map.put(menu.getId(), menu);
        }
        ArrayList<Menu> menuTree = new ArrayList<>();
        for (Menu menu : menus) {
            if (menu.getParentId() == null) {
                menuTree.add(menu);
            } else {
                Long parentId = menu.getParentId();
//               找到父级菜单
                Menu parenMenu = map.get(parentId);
                if (parenMenu != null) {
                    parenMenu.getChildren().add(menu);

                }


            }

        }
        return menuTree;


    }
}
